<?php

namespace App\Models\Rbac;

use App\Models\Admin;
use DateTimeInterface;
use Spatie\Permission\Models\Role as SpatieRole;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

class Role extends SpatieRole
{

//    protected $fillable = [
//        'name',
//        'slug',
//        'parent_id',
//        'status'
//    ];

//    public function admins()
//    {
//        return $this->belongsToMany(Admin::class, 'admin_roles');
//    }

//    public function menus()
//    {
//        return $this->belongsToMany(Menu::class, 'role_menus');
//    }

    /**
     * 获取列表数据
     * @param $params
     * @return LengthAwarePaginator
     */
    public static function getQueryData($params): LengthAwarePaginator
    {
        return self::query()->when($params['slug'] ?? false, function ($query, $value) {
            return $query->where('slug', $value);
        })->when($params['name'] ?? '' ,function ($query,$value){
            return $query->where('name','like',"%{$value}%");
        })->when($params['status'] ?? false,function($query,$value){
            return $query->where('status',$value);
        })->with('menus:id')->paginate(request('pageSize',20));
    }

    /**
     * 格式化时间
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }
}
